package com.neuedu.javase.tools.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.tools.date 
 * date:   2025/10/31   8:37  
 * 描述:  使用时区
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class UseTimeZone {
    public static void main(String[] args) {

    //     Date 代表瞬时时间  没有国际化


        //Calendar
        Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("Africa/Abidjan"));
        Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai"));
        formatOutput(cal1);
        formatOutput(cal2);

        // cal1.setTimeZone();


        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // System.out.println("sdf.format( cal1.getTime()) = " + sdf.format(cal1.getTime()));
        // System.out.println("sdf.format( cal2.getTime()) = " + sdf.format(cal2.getTime()));

        System.out.println("======================有效时区=========================");
        // String[] availableIDs = TimeZone.getAvailableIDs();
        // for (String availableID : availableIDs) {
        //     System.out.println("availableID = " + availableID);
        // }


    }



    /**
     * 格式化输出
     * @param calendar
     * @return
     */
    public static  void  formatOutput(Calendar calendar){
        //     获取字段
        int year = calendar.get(Calendar.YEAR);
        //月份从0 开始  0--11
        int month = calendar.get(Calendar.MONTH) + 1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        int hour = calendar.get(Calendar.HOUR);   //12 小时
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);  //24 时

        // CTRL+SHIFT+ U 切换大小写
        int minutes = calendar.get(Calendar.MINUTE);  //24 时
        int seconds = calendar.get(Calendar.SECOND);  //24 时

        String format =  year+"-"+month+"-"+dayOfMonth+" "+hourOfDay +":"+minutes+":"+seconds+"  星期"+dayOfWeek;
        System.out.println(format);

    }

}
